package com.spica.spicaschool.viewmodel

import androidx.lifecycle.LiveData
import com.rxjava.rxlife.LifecycleScope
import com.spica.spicaschool.base.LiveCoroutinesViewModel
import com.spica.spicaschool.bean.User
import com.spica.spicaschool.bean.UserBody
import com.spica.spicaschool.network.repository.UserRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import timber.log.Timber
import javax.inject.Inject


@HiltViewModel
class UserInfoViewModel @Inject constructor(
    private val userRepository: UserRepository,
) : LiveCoroutinesViewModel() {


    private lateinit var _userLiveData: LiveData<User?>


    val userLiveData: LiveData<User?>
        get() = _userLiveData

    private lateinit var _userBodyLiveData: LiveData<UserBody?>

    val userBodyLiveData: LiveData<UserBody?>
        get() = _userBodyLiveData


    private lateinit var _updateUserResult: LiveData<String?>

    val updateUserResult: LiveData<String?>
        get() = _updateUserResult


    fun fetchUserInfo(
        token: String,
        onComplete: () -> Unit,
        onError: (String?) -> Unit
    ) {
        _userLiveData =
            userRepository.fetchInfo(token, onComplete, onError)
                .asLiveDataOnViewModelScope()//绑定生命周期
    }

    fun fetchUserBodyInfo(token: String) {
        _userBodyLiveData = userRepository.fetchUserBodyInfo(token).asLiveDataOnViewModelScope()
    }

    fun updateUserInfo(
        token: String,
        head: String? = null,
        nickname: String? = null,
        selfdes: String? = null,
        gender: Int? = null,
        birthday: String? = null,
        height: Int? = null,
        weight: Int? = null,
        vialCap: Int? = null,
        onComplete: () -> Unit,
        onError: (String?) -> Unit
    ) {
        _updateUserResult = userRepository.updateUserInfo(
            token,
            head,
            nickname,
            selfdes,
            gender,
            birthday,
            height,
            weight,
            vialCap,
            onComplete,
            onError
        ).asLiveDataOnViewModelScope()
    }


}